package com.business.log;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.beans.log.LogType;
import com.sql.DBFactory;

public class LogTypeBus {

	private Connection conn = null;

	private Statement stmt = null;

	private ResultSet rs = null;

	private static final Log log = LogFactory.getLog(LogTypeBus.class);

	public void add(LogType lt) {
		String typeName = lt.getLogtypeName();
		String userName = lt.getUsername();
		String typeMemo = lt.getLogtypeMemo();
		String sql = "insert into log_type(logtype_name,username,logtype_memo)values('";
		sql += typeName + "','" + userName + "','" + typeMemo + "')";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the insert loginfo exception:" + e.getMessage());
			log.error("the insert loginfo sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
	}

	public void del(String typeId) {
					String sql = "delete from log_type where logtype_id=" + typeId;
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);

		} catch (Exception e) {
			log.error("the select loginfo exception:" + e.getMessage());
			log.error("the select loginfo sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		
		}


	public void update(LogType lt, String typeId) {
		String typeName = lt.getLogtypeName();
		String userName = lt.getUsername();
		String typeMemo = lt.getLogtypeMemo();
		String sql = "update log_type set logtype_name='" + typeName
				+ "',username='" + userName + "',logtype_memo='" 
				+ typeMemo + "' where logtype_id="+typeId;
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the insert loginfo exception:" + e.getMessage());
			log.error("the insert loginfo sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
	}
	@SuppressWarnings("unchecked")
	public List getAllLogTypes(String username) {
		List list = new ArrayList();
		String sql = "select * from log_type where username='" + username + "'";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				LogType lt = new LogType();
				lt.setLogtypeMemo(rs.getString("logtype_memo"));
				lt.setLogtypeName(rs.getString("logtype_name"));
				lt.setLogtypeId(rs.getString("logtype_id"));
				list.add(lt);
			}
		} catch (Exception e) {
			log.error("the select logtype exception:" + e.getMessage());
			log.error("the select logtype sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return list;
	}

	public void updateLogtype(LogType lt, String logtypeId) {
		String typeName = lt.getLogtypeName();
		String userName = lt.getUsername();
		String typeMemo = lt.getLogtypeMemo();
		String sql = "update log_type set logtype_name='" + typeName;
		sql += "',username='" + userName + "',logtype_memo='";
		sql += typeMemo + "' where logtype_id=" + Integer.parseInt(logtypeId);
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the update logtype exception:" + e.getMessage());
			log.error("the update logtype sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

		
	public LogType getLogTypeById(int id) {
		LogType lt = new LogType();
		String sql = "select * from log_type where logtype_id=" + id;
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				lt.setLogtypeId(rs.getString("logtype_id"));
				lt.setLogtypeMemo(rs.getString("logtype_memo"));
				lt.setLogtypeName(rs.getString("logtype_name"));
				lt.setUsername(rs.getString("username"));
			}
		} catch (Exception e) {
			log.error("the select logtype exception:" + e.getMessage());
			log.error("the select logtype sql is: " + sql);
		} finally {
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}
		return lt;
	}


	

}
